<?php

include_once ("TaskHubDB.class.php");
include_once ("Account.class.php");
include_once ("WPNotification.class.php");


class Task {
	/*
        public TaskList AddTask(uint OwnerAccountID, uint TargetAccountID, string Name, DateTime DueDate, string Description)
        public bool DeleteTask(uint MyAccountID, uint TaskID)
        public bool AcceptTask(uint MyAccountID, uint TaskID)
        public bool RejectTask(uint MyAccountID, uint TaskID)
        public uint CompleteTask(uint MyAccountID, uint TaskID)
        public bool ModifyTask(uint TaskID, uint TargetAccountID, string Name, DateTime DueDate, string Description)
        public TaskList[] GetMyTasks(uint MyAccountID)
        public TaskList[] GetOthersTasks(uint MyAccountID)

		public class TaskList
		{
			public uint TaskID { get; set; }
			public uint OwnerAccountID { get; set; }
			public uint TargetAccountID { get; set; }
			public string Name { get; set; }
			public DateTime DueDate { get; set; }
			public string Description { get; set; }
			public char Status { get; set; }
		}
	*/

	//////////////////////////////////////////////////////////////////////////////////////////////////
	//  Public WS methods

	public function AddTask ($data) {
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_AddTask (
			$params["OwnerAccountID"], $params["TargetAccountID"], $params["Name"], $params["DueDate"], $params["Description"]
		));
	}

	public function DeleteTask ($data) {
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_DeleteTask (
			$params["MyAccountID"], $params["TaskID"]
		));
	}

	public function AcceptTask ($data) {
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_AcceptTask (
			$params["MyAccountID"], $params["TaskID"]
		));
	}

	public function RejectTask ($data) {
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_RejectTask (
			$params["MyAccountID"], $params["TaskID"]
		));
	}

	public function CompleteTask ($data) {
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_CompleteTask (
			$params["MyAccountID"], $params["TaskID"]
		));
	}

	public function ModifyTask ($data) {
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_ModifyTask (
			$params["TaskID"], $params["TargetAccountID"], $params["Name"], $params["DueDate"], $params["Description"]
		));
	}

	public function GetMyTasks ($data) {
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_GetMyTasks (
			$params["MyAccountID"]
		));
	}

	public function GetOthersTasks ($data) {
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_GetOthersTasks (
			$params["MyAccountID"]
		));
	}


	//////////////////////////////////////////////////////////////////////////////////////////////////
	//  Private raw methods

	private function _GetTaskInfo ($TaskID) {
		$result = mysql_query ("SELECT * FROM Tasks WHERE TaskID = '{$TaskID}'");
		if (! $result) {
			TaskHubDB::AddError (mysql_error ());
			return null;
		}

		return mysql_fetch_assoc ($result);
	}

	private function _AddTask ($OwnerAccountID, $TargetAccountID, $Name, $DueDate, $Description) {
		// Decide the status //
		$status = '0'; // Waiting
		if ($OwnerAccountID == $TargetAccountID)
			$status = 'A';

		// Insert new Task //
		$query = "INSERT INTO Tasks (OwnerAccountID,TargetAccountID,Description,DueDate,ObservationText,StatusID) VALUES " .
				 "('{$OwnerAccountID}', '{$TargetAccountID}', '{$Name}', '{$DueDate}', '{$Description}', '{$status}')";
		$result = mysql_query ($query);
		if (! $result) {
			TaskHubDB::AddError (mysql_error ());
			return false;
		}

		// Notify the Target //
		if ($OwnerAccountID != $TargetAccountID) {
			$notification = new WPNotification ($TargetAccountID);
			$notification->SendToast ("Nova tarefa!", $Name);
			$notification->SendRaw ();
			$notification->SendTileUpdate ();
		}

		$task_id = mysql_insert_id ();
		return Array (
			"TaskID" => $task_id,
			"OwnerAccountID" => $OwnerAccountID,
			"TargetAccountID" => $TargetAccountID,
			"Name" => $Description,
			"DueDate" => $DueDate,
			"Description" => $ObservationText,
			"Status" => $status
		);
	}

	private function _DeleteTask ($MyAccountID, $TaskID) {
		// Get Task details //
		$task = $this->_GetTaskInfo ($TaskID);
		if ($task == null)
			return false;

		// Only delete if this Task is yours //
		if ($task["OwnerAccountID"] != $MyAccountID)
			return false;

		$result = mysql_query ("DELETE FROM Tasks WHERE TaskID = '{$TaskID}'");
		if (! $result) {
			TaskHubDB::AddError (mysql_error ());
			return false;
		}

		// Notify the Target //
		if ($OwnerAccountID != $TargetAccountID) {
			$notification = new WPNotification ($TargetAccountID);
			$notification->SendToast ("Tarefa cancelada!", $Name);
			$notification->SendRaw ();
			$notification->SendTileUpdate ();
		}

		return true;
	}

	private function _ChangeTaskStatus ($TaskID, $Status) {
		$result = mysql_query ("UPDATE Tasks SET StatusID = '{$Status}' WHERE TaskID = '{$TaskID}'");
		if (! $result) {
			TaskHubDB::AddError (mysql_error ());
			return false;
		}

		return true;
	}

	private function _AcceptTask ($MyAccountID, $TaskID) {
		// Get Task details //
		$task = $this->_GetTaskInfo ($TaskID);
		if ($task == null)
			return false;

		// Only accept if this Task is meant for you //
		if ($task["TargetAccountID"] != $MyAccountID)
			return false;

		// Only accept if Task is waiting //
		if ($task["Status"] == "0") {
			if (! $this->_ChangeTaskStatus ($TaskID, "A"))
				return false;
		}
		else {
			return false;
		}

		// Notify the owner
		$notification = new WPNotification ($OwnerAccountID);
		$notification->SendToast ("Tarefa aceita!", $Name);
		$notification->SendRaw ();
		$notification->SendTileUpdate ();

		return true;
	}

	private function _RejectTask ($MyAccountID, $TaskID) {
		// Get Task details //
		$task = $this->_GetTaskInfo ($TaskID);
		if ($task == null)
			return false;

		// Only reject if this Task is meant for you //
		if ($task["TargetAccountID"] != $MyAccountID)
			return false;

		// Only reject if Task is waiting or accepted//
		if ($task["StatusID"] == "0" || $task["StatusID"] == "A") {
			if (! $this->_ChangeTaskStatus ($TaskID, "R"))
				return false;
		}
		else {
			return false;
		}

		// Notify the owner
		$notification = new WPNotification ($TargetAccountID);
		$notification->SendToast ("Tarefa recusada!", $Name);
		$notification->SendRaw ();
		$notification->SendTileUpdate ();

		return true;
	}

	private function _CompleteTask ($MyAccountID, $TaskID) {
		// Get Task details //
		$task = $this->_GetTaskInfo ($TaskID);
		if ($task == null)
			return 0;
/*
		// Only complete if this Task is meant for you //
		if ($task["TargetAccountID"] != $MyAccountID)
			return 0;

		// Only complete if Task is accepted//
		if ($task["StatusID"] == "A") {
			if (! $this->_ChangeTaskStatus ($TaskID, "C"))
				return 0;
		}
		else {
			return 0;
		}
*/
		mysql_query ("DELETE FROM Tasks WHERE TaskID = '{$TaskID}'");

		// Notify the owner
		$notification = new WPNotification ($TargetAccountID);
		$notification->SendToast ("Tarefa concluida!", $Name);
		$notification->SendRaw ();
		$notification->SendTileUpdate ();

		return $TaskID;
	}

	private function _ModifyTask ($TaskID, $TargetAccountID, $Name, $DueDate, $Description) {
		// Get Task details //
		$task = $this->_GetTaskInfo ($TaskID);
		if ($task == null)
			return false;

		// Only modify if this Task is yours //
		if ($task["OwnerAccountID"] != $MyAccountID)
			return false;

		// Only modify if Task is rejected //
		if ($task["Status"] != "R")
			return false;

		// Modify the Task //
		$query = "UPDATE Tasks SET TargetAccountID = '{$TargetAccountID}', Description = '{$Name}', DueDate = '{$DueDate}', ObservationText = '{$Description}' " .
				 "WHERE TaskID = '{$TaskID}'";
		$result = mysql_query ($query);
		if (! $result) {
			TaskHubDB::AddError (mysql_error ());
			return false;
		}

		return true;
	}

	private function _GetMyTasks ($MyAccountID) {
		// Gets all your tasks //
		//$result = mysql_query ("SELECT TaskID,OwnerAccountID,TargetAccountID,Description,DueDate,ObservationText,StatusID FROM Tasks WHERE TargetAccountID = '$MyAccountID'");
		$result = mysql_query ("SELECT TaskID,OwnerAccountID,TargetAccountID,Description,DueDate,ObservationText,StatusID FROM Tasks WHERE '1'");
		if (! $result) {
			TaskHubDB::AddError (mysql_error ());
			return false;
		}

		// Map the result to the WS result //
		$return_array = Array ();
		while ($row = mysql_fetch_assoc ($result)) {
			$resurn_array[] = Array ( "TaskID" => $row["TaskID"] );
		/*
			$resurn_array[] = Array (
				"TaskID" => $row["TaskID"],
				"OwnerAccountID" => $row["OwnerAccountID"],
				"TargetAccountID" => $row["TargetAccountID"],
				"Name" => $row["Description"],
				"DueDate" => $row["DueDate"],
				"Description" => $row["ObservationText"],
				"Status" => $row["StatusID"]
			);
		*/
		}

		return $return_array;
	}

	private function _GetOthersTasks ($MyAccountID) {
		// Gets all the tasks you created //
		$result = mysql_query ("SELECT TaskID,OwnerAccountID,TargetAccountID,Description,DueDate,ObservationText,StatusID FROM Tasks WHERE OwnerAccountID = '{$MyAccountID}'");
		if (! $result) {
			TaskHubDB::AddError (mysql_error ());
			return false;
		}

		// Map the result to the WS result //
		while ($row = mysql_fetch_assoc ($result)) {
			$resurn_array[] = Array (
				"TaskID" => $row["TaskID"],
				"OwnerAccountID" => $row["OwnerAccountID"],
				"TargetAccountID" => $row["TargetAccountID"],
				"Name" => $row["Description"],
				"DueDate" => $row["DueDate"],
				"Description" => $row["ObservationText"],
				"Status" => $row["StatusID"]
			);
		}

		$return_array = Array ();
		return $return_array;
	}

}

?>